/******************************************************************************
 * Author:      671643387
 * Created:     2016/10/11
 *****************************************************************************/

var mjMark = require('../mjmark.js');

///////////////////////////////////////////////////////////////////////////////
//>> 计分标识
//鸡
var PLAY_MARK_CHARGE_JI              = 301;        //冲锋鸡
var PLAY_MARK_RESPONSIBILITY_JI      = 302;        //责任鸡
var PLAY_MARK_PENG_RESPONSIBILITY_JI = 303;        //碰冲锋鸡
var PLAY_MARK_ORDINARY_JI            = 304;        //普通鸡
var PLAY_MARK_CATCH_JI               = 305;        //捉到的鸡
var PLAY_MARK_GOLD_JI                = 306;        //金鸡    胡牌后翻出来幺鸡
var PLAY_MARK_BLACK_JI               = 307;        //乌骨鸡  8筒
var PLAY_MARK_HANBAO_DOU             = 308;        //憨包豆

// -- 贵阳麻将
function SubGame(owner) {
    this.owner                = owner;  // 游戏房间

    this.isPlayCharge         = false;  // 房间内有没有冲锋鸡

    this.curRoomChickenCard   = 0;   // 当前房间胡牌后摸的第一张鸡牌，
}

SubGame.prototype = {

    /**
     * 初始化数据
     * @param firstRound    是不是第一轮
     */
    onInit: function(firstRound) {
        this.isPlayCharge = false;
        this.curRoomChickenCard = 0;
    },

    /**
     * 有玩家胡牌后游戏能否继续
     * @returns {boolean}
     */
    canGameContinueWhenPlayerHuCard: function() {
        return false;
    },

    /**
     * 有人加入房间
     * @param playerIndex
     * @param player
     */
    onPlayerJoinedRoom: function(playerIndex, player) {

    },

    /**
     * 玩家出牌
     * @param player
     * @param card
     */
    onPlayerPlayCard: function(player, card) {

    },

    /**
     * 房间没有冲锋鸡，玩家放弃碰或者杠幺鸡
     * @param player    点过的玩家
     */
    onPlayerPassGangOrPengAction: function(player) {

        if ((player.tips.gang == 10 || player.tips.peng == 10) && !this.isPlayCharge) {
            var cunPlayer = this.owner.findRoomCunPlayerHandr();

            var playerIndex = this.owner.getPlayer(cunPlayer);

            //冲锋鸡出现
            if(!this.isPlayCharge){
                this.isPlayCharge = true;
                cunPlayer.markPush(PLAY_MARK_CHARGE_JI, 10);

                this.owner.boradcastMessage('setPlayerMark', {
                    mark: PLAY_MARK_CHARGE_JI,
                    playerIndex: playerIndex
                });
            }else{
                cunPlayer.markPush(PLAY_MARK_ORDINARY_JI, 10);
            }
        }
    },

    onCunPlayerPlayCardAction: function () {

        var cunRoomCard = this.owner.roundInfo.curMahjong;
        var cunPlayer = this.owner.findRoomCunPlayerHandr();

        if (cunRoomCard.value == 10 && cunPlayer) {
            //冲锋鸡出现
            if(!this.isPlayCharge){
                this.isPlayCharge = true;
                cunPlayer.markPush(PLAY_MARK_CHARGE_JI, 10);

                var playerIndex = this.owner.getPlayer(cunPlayer.uid);

                this.owner.boradcastMessage('setPlayerMark', {
                    mark: PLAY_MARK_CHARGE_JI,
                    playerIndex: playerIndex
                });
            }else{
                cunPlayer.markPush(PLAY_MARK_ORDINARY_JI, 10);
            }
        }
    },

    /**
     * 玩家杠牌
     * @param player
     * @param card          玩家杠的牌字面值
     * @param gangNewCard   玩家杠的是不是刚摸的牌
     */
    onPlayerGangCard: function(player, card, gangNewCard) {
        if (!gangNewCard) {
            player.markPush(PLAY_MARK_HANBAO_DOU, card.value);
        } else {
            player.markPush(mjMark.PLAY_MARK_DARK_GANG, card.value);
        }
    },

    /**
     * 玩家出牌后其他玩家不能有碰牌/杠牌等操作时调用
     * @param player    玩家
     * @param card      操作的牌
     */
    onPlayerPlayedCardAndNoOtherAction: function(player, card, playerIndex) {
        var room = this.owner;

        //当前牌是幺鸡
        if(card.value == 10) {
            var flag = null;

            //当前房间还没有出现冲锋鸡
            if (!this.isPlayCharge) {
                flag = PLAY_MARK_CHARGE_JI;

                //冲锋鸡出现
                this.isPlayCharge = true;
                room.boradcastMessage('setPlayerMark', {
                    mark: flag,
                    playerIndex: playerIndex
                });
            } else {
                flag = PLAY_MARK_ORDINARY_JI;
            }
            player.markPush(flag, card.value);
        }
    },

    /**
     *  责任鸡分别对出牌玩家、碰牌玩家加标示
     * @param curPlayer     当前操作牌的玩家
     * @param card          操作的牌
     * @param pengPlayer    当前出牌的玩家
     */
    onPlayerPengAction: function (curPlayer, card, coverPengPlayer) {
        if(!this.isPlayCharge && card.value == 10){
            //给玩家加责任鸡标示
            coverPengPlayer.markPush(PLAY_MARK_RESPONSIBILITY_JI, card.value);

            //碰牌玩家加两张跟随鸡标示 && 一张碰冲锋鸡的标示
            curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
            curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
            curPlayer.markPush(PLAY_MARK_PENG_RESPONSIBILITY_JI, card.value);

            //冲锋鸡失效，后面出来的鸡都不是了
            this.isPlayCharge = true;

            var playerIndex = this.owner.getPlayer(coverPengPlayer.uid);
            this.owner.boradcastMessage('setPlayerMark', {
                mark: PLAY_MARK_RESPONSIBILITY_JI,
                playerIndex: playerIndex
            });
        }else if(card.value == 10){
            //碰牌玩家加两张普通鸡标示 && 出牌玩家一张碰普通鸡的标示
            coverPengPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
            curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
            curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
        }
    },
    /**
     *  责任鸡分别对出牌玩家、杠牌玩家加标示
     * @param curPlayer     当前操作牌的玩家
     * @param card          操作的牌
     * @param pengPlayer    当前出牌的玩家
     */
    onPlayerGangAction: function (curPlayer, card, coverPengPlayer) {
        if(!this.isPlayCharge && card.value == 10){
            if(curPlayer.newMahjong.value == 0) {
                //点杠鸡牌
                //给玩家加责任鸡标示
                coverPengPlayer.markPush(PLAY_MARK_RESPONSIBILITY_JI, card.value);

                curPlayer.markPush(PLAY_MARK_PENG_RESPONSIBILITY_JI, card.value);
                curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
                curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
                curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);

                //碰牌玩家加两张跟随鸡标示 && 一张碰冲锋鸡的标示
                var playerIndex = this.owner.getPlayer(coverPengPlayer.uid);
                this.owner.boradcastMessage('setPlayerMark', {
                    mark: PLAY_MARK_RESPONSIBILITY_JI,
                    playerIndex: playerIndex
                });
            }else{
                //暗杠鸡牌
                var flag = curPlayer.findMark(PLAY_MARK_PENG_RESPONSIBILITY_JI);
                console.log("guiyang fil: onPlayerGangAction flag = " + flag);
                if(!curPlayer.findMark(PLAY_MARK_PENG_RESPONSIBILITY_JI)){
                    curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
                    curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
                    curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
                    curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
                }else {
                    curPlayer.markPush(PLAY_MARK_ORDINARY_JI, card.value);
                }
            }
            //冲锋鸡失效，后面出来的鸡都不是了
            this.isPlayCharge = true;
        }
    },

    /**
     * 检查玩家能否和牌
     * @param player
     * @param me  是否自摸
     */
    onCheckPlayerHuPai: function(player, me) {
        if(player.isBaoTing){
            return true;
        }

        if (player.hasHuPaiType(mjMark.HUCARD_TYPE_PING) && !me) {
            // 如果是平糊，必要要有豆
            return player.hasGangs();
        }
        return true;
    },

    onSettlementCurrentGame: function() {
        var room = this.owner;
        var baseScore = room.game.baseScore;    // 底分
        var multiple = room.game.multiple;      // 番数

        // 检查玩家听牌
        var numTingPlayer = 0;
        room.enumerationPlayer(function(ePlayerIdx, ePlayer){
            ePlayer.checkTingPai();
            if (ePlayer.isTing()) {
                numTingPlayer += 1;
            }
        });

        var isLiuJu = false;

        // 检查是否流局
        if (!room.roundInfo.isRoomHuPai && room.mahjongs.getCardNum() == 0) {
            // 流局
            isLiuJu = true;

            if (numTingPlayer == 0) {

            } else {
                for (var playerIdx in room.players) {
                    if (!room.players.hasOwnProperty(playerIdx)) {
                        continue;
                    }
                    var cunPlayer = room.players[playerIdx];
                    if(cunPlayer.isTing()){
                        var huPaiMethod = cunPlayer.getHuPaiType();
                        var numMethod = 0;
                        huPaiMethod.forEach(function (method) {
                            //听牌最大牌型
                            numMethod = numMethod > method ? numMethod : method;
                        });
                        room.enumerationPlayer(function(ePlayerIdx, ePlayer){
                            //听牌玩家加分， 未听牌玩家扣分
                            if( cunPlayer == ePlayer || ePlayer.isTing()){
                                return;
                            }
                            var playerBaseScore = this.getHuPaiTypeBaseScore(cunPlayer);

                            cunPlayer.incIntegral(numMethod, playerBaseScore);
                            ePlayer.decIntegral(numMethod, playerBaseScore);
                        }.bind(this));
                    }
                }
            }
        } else {
            // 定鸡牌
            this.onSelectCardAction();

            // 捉鸡
            room.enumerationPlayer(function(ePlayerIdx, ePlayer){
                this.checkRoomPlayerIsCardAction(ePlayer);
            }.bind(this));

            // 计算胡牌方式
            for (var playerIdx in room.players) {
                if (!room.players.hasOwnProperty(playerIdx)) {
                    continue;
                }

                var ePlayer = room.players[playerIdx];
                if (!ePlayer.isHu()) {
                    continue;
                }

                var playerBaseScore = this.getHuPaiTypeBaseScore(ePlayer);

                if (ePlayer.hasHuPaiMethod(mjMark.HUCARD_METHOD_ZIMO)) {
                    // 玩家自摸

                    // 计算单家付分
                    var playerWinScore = playerBaseScore;
                    playerWinScore *= multiple;
                    playerWinScore += (baseScore * multiple);

                    var integral = 0;
                    var numPlayer = 1;
                    if(ePlayer.getBaoTing()){       //自摸并之前是报听
                        playerWinScore *= 10;
                    }else if(ePlayer.getKillBao()){ //自摸杀报，扣报听玩家十倍，后面扣分玩家数量减一
                        integral += playerWinScore * 10;
                        numPlayer += 1;
                    }

                    integral += playerWinScore * (this.owner.joinedPlayers - numPlayer);

                    // 给玩家加分
                    ePlayer.incIntegral(mjMark.HUCARD_METHOD_ZIMO, integral);
                    ePlayer.incHuPaiScore(integral);

                    // 扣除其他玩家积分
                    this.decPlayerIntegral(mjMark.HUCARD_METHOD_ZIMO, playerWinScore, [ePlayer.uid], ePlayer.getBaoTing());
                    break;
                }

                if (ePlayer.hasHuPaiMethod(mjMark.HUCARD_METHOD_GANGKAI)) {
                    // 玩家杠开

                    // 计算单家付分
                    var playerWinScore = playerBaseScore;
                    playerWinScore *= multiple;
                    playerWinScore += (baseScore * multiple * 2);


                    var integral = 0;
                    var numPlayer = 1;

                    if(ePlayer.getBaoTing()){       //自摸并之前是报听
                        playerWinScore *= 10;
                    }else if(ePlayer.getKillBao()){ //自摸杀报，扣报听玩家十倍，后面扣分玩家数量减一
                        integral += playerWinScore * 10;
                        numPlayer += 1;
                    }

                    integral += playerWinScore * (this.owner.joinedPlayers - numPlayer);

                    // 给玩家加分
                    ePlayer.incIntegral(mjMark.HUCARD_METHOD_GANGKAI, integral);
                    ePlayer.incHuPaiScore(integral);

                    // 扣除其他玩家积分
                    this.decPlayerIntegral(mjMark.HUCARD_METHOD_GANGKAI, playerWinScore, [ePlayer.uid], ePlayer.getBaoTing());
                    break;
                }

                if (ePlayer.hasHuPaiMethod(mjMark.HUCARD_METHOD_REPAO)) {
                    // 热炮

                    // 计算单家付分
                    var playerWinScore = playerBaseScore;
                    playerWinScore *= multiple;
                    playerWinScore += (baseScore * multiple);

                    // 扣除点炮玩家积分
                    var dianPaoIsBaoTing = this.decPlayerIntegralWhoHasHuiPaiMethod(ePlayer, playerWinScore, mjMark.HUCARD_METHOD_REPAO);

                    if(dianPaoIsBaoTing){  // 点炮者报听，胡牌玩家杀报
                        playerWinScore *= 10;
                        ePlayer.incIntegral(mjMark.HUCARD_METHOD_KILL_BAO, playerWinScore);
                    }else if(ePlayer.getBaoTing()){ //胡牌玩家报听
                        playerWinScore *= 10;
                        ePlayer.incIntegral(mjMark.HUCARD_METHOD_BAO_TING, playerWinScore);
                    }else {
                        // 给玩家加分
                        ePlayer.incIntegral(mjMark.HUCARD_METHOD_REPAO, playerWinScore);
                    }
                    ePlayer.incHuPaiScore(playerWinScore);

                    continue;
                }

                if (ePlayer.hasHuPaiMethod(mjMark.HUCARD_METHOD_DIANPAO)) {
                    // 点炮

                    // 计算单家付分
                    var playerWinScore = playerBaseScore;
                    playerWinScore *= multiple;

                    // 扣除点炮玩家积分
                    var dianPaoIsBaoTing = this.decPlayerIntegralWhoHasHuiPaiMethod(ePlayer, playerWinScore, mjMark.HUCARD_METHOD_DIANPAO);

                    // 给玩家加分
                    if(dianPaoIsBaoTing){ // 点炮者报听，胡牌玩家杀报
                        playerWinScore *= 10;
                        ePlayer.incIntegral(mjMark.HUCARD_METHOD_KILL_BAO, playerWinScore);
                    }else if(ePlayer.getBaoTing()){ //胡牌玩家报听
                        playerWinScore *= 10;
                        ePlayer.incIntegral(mjMark.HUCARD_METHOD_BAO_TING, playerWinScore);
                    }else {
                        ePlayer.incIntegral(mjMark.HUCARD_METHOD_DIANPAO, playerWinScore);
                    }
                    ePlayer.incHuPaiScore(playerWinScore);


                    continue;
                }
            }

            // 特定游戏结算
            this.settlementGeneralCardAction();

            // 杠牌结算
            this.settlementGangCardAction();

        }
        // 发送结算结果
        var settlementRes = {
            isLiuJu: isLiuJu,
            isLastRound: (room.game.totalRound == room.roundInfo.round) || room.isSureToDestroy,
            jiCard: this.curRoomChickenCard,
            players: {},
        };
        room.enumerationPlayer(function(ePlayerIdx, ePlayer){
            settlementRes.players[ePlayerIdx] = ePlayer.getSettlementInfo();
        });
        if(!settlementRes){
            settlementRes = null;
        }
        room.boradcastMessage('setSettlement', settlementRes);
    },

    getHuPaiTypeBaseScore: function(player) {
        var baseScore = this.owner.game.baseScore;
        var integral = 0;
        if (player.hasHuPaiType(mjMark.HUCARD_TYPE_QINGDADUI)) {
            integral += 30 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_QINGLONGBEI)) {
            integral += 30 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_LONGQIDUI)) {
            integral += 20 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_QINGQIDUI)) {
            integral += 20 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_DANDIAO)) {
            integral += 10 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_QINGYISE)) {
            integral += 10 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_QIDUI)) {
            integral += 10 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_DADUI)) {
            integral += 5 * baseScore;
        } else if (player.hasHuPaiType(mjMark.HUCARD_TYPE_PING)) {
            integral += baseScore;
        }
        return integral;
    },

    decPlayerIntegral: function(what, num, excludes, huPlayerIsBaoTing) {
        excludes = excludes || [];
        this.owner.enumerationPlayer(function(ePlayerIdx, ePlayer){
            if (excludes.indexOf(ePlayer.uid) != -1) {
                return;
            }
            if(ePlayer.getBaoTing() && !huPlayerIsBaoTing){ // 被杀报 与上 胡牌玩家没有报听
                num *= 10;
            }
            ePlayer.decIntegral(what, num);
            ePlayer.decHuPaiScore(num);
        });
    },

    decPlayerIntegralWhoHasHuiPaiMethod: function(huPlayer, num, method) {
        var dianPaoIsBaoTing = false;
        var baseScore = this.owner.game.baseScore;    // 底分
        var multiple = this.owner.game.multiple;      // 番数
        this.owner.enumerationPlayer(function(ePlayerIdx, ePlayer){
            if (!ePlayer.isHu() && ePlayer.hasHuPaiMethod(method) && (ePlayer.uid == this.owner.roundInfo.dianPaoUid)) {
                if(ePlayer.getBaoTing() && huPlayer.getKillBao()){ // 被杀报 或者胡牌玩家之前报听
                    dianPaoIsBaoTing = true;
                    num *= 10;
                }else if(huPlayer.getBaoTing()){
                    num *= 10;
                }
                ePlayer.decIntegral(method, num);
                ePlayer.decHuPaiScore(num);
                return false;
            }
        }.bind(this));
        return dianPaoIsBaoTing;
    },

    /**
     * 定鸡牌
     */
    onSelectCardAction:function () {
        var room = this.owner;

        //翻开胡牌后第1张未摸的牌，根据这张牌的数值，顺位+1的那张牌即为“鸡”，普通鸡。
        var card = this.owner.mahjongs.dealCards(1)[0];

        var selectCard = card.value + 1;
        if (selectCard > 9) {
            selectCard = 1;
        }
        selectCard += (card.type - 1) * 9;

        this.curRoomChickenCard = selectCard;

        room.boradcastMessage('setRoomChickenCard',{
            card: selectCard
        });
    },

    /**
     * 检查房间内玩家是否有捉到的鸡牌,
     * @param player    玩家
     */
    checkRoomPlayerIsCardAction:function(player){
        var room = this.owner;
        var flag = null;

        if(this.curRoomChickenCard == 10) {             //金鸡，胡牌后翻出来幺鸡
            flag = PLAY_MARK_GOLD_JI;
        }else if(this.curRoomChickenCard == 8){         //乌骨鸡，8筒
            flag = PLAY_MARK_BLACK_JI;
        }else{                                  //胡牌后翻出来的普通鸡
            flag = PLAY_MARK_CATCH_JI;
        }

        //玩家手牌中的鸡牌
        var handCards = player.getHandCards();
        handCards.cards.forEach(function (card) {
            if(card.value == this.curRoomChickenCard && card.num > 0) {
                for(var index = 0; index < card.num; ++index) {
                    player.markPush(flag, card.value);
                }
            }
        }.bind(this));

        var gangs = player.getGangs();
        gangs.forEach(function(gang){
            if(gang[0] == this.curRoomChickenCard) {
                player.markPush(flag, gang[0]);
                player.markPush(flag, gang[0]);
                player.markPush(flag, gang[0]);

                var card = 0;
                //如果捉的鸡牌在中，检查杠牌是否是暗杠，暗杠标志多加一个
                player.marks.forEach(function (mark) {
                    if(mark.key == mjMark.PLAY_MARK_DARK_GANG) {
                        card = mark.value;
                    }
                });
                if(card == this.curRoomChickenCard ) {
                    player.markPush(flag, gang[0]);
                }
            }
        }.bind(this));

        var pengs = player.getPengs();
        pengs.forEach(function(peng){
            if(peng[0] == this.curRoomChickenCard) {
                player.markPush(flag, peng[0]);
                player.markPush(flag, peng[0]);
            }
        }.bind(this));

        //打出去牌中的鸡牌
        var numCard = room.roundInfo.dealedMahjons.find(player.uid, this.curRoomChickenCard);
        for(var index = 0; index < numCard; ++index){
            player.markPush(flag, this.curRoomChickenCard);
        };

    },

    /**
     * 结算鸡牌
     */
    settlementGeneralCardAction: function () {
        var room = this.owner;
        if(!room) {
            return ;
        }

        var baseScore = room.game.baseScore;    // 底分
        var multiple = room.game.multiple;      // 番数

        for (var key in room.players) {
            if (!room.players.hasOwnProperty(key)) {
                continue;
            }
            var cunPlay = room.players[key];
            var isPengChargeJi = false;
            var isResponsibilityJi = false;
            var integral = 0;
            //玩家听牌， 并且没有热炮
            if (cunPlay.isTing() && !cunPlay.hasHuPaiMethod(mjMark.HUCARD_METHOD_REPAO)) {
                //听牌条件下，检查自己的鸡结算积分
                cunPlay.marks.forEach(function (mark) {
                    switch (mark.key) {
                        case PLAY_MARK_CHARGE_JI://冲锋鸡
                            integral = ((baseScore * multiple) * (room.joinedPlayers - 1)) * 2;
                            cunPlay.incIntegral(mark.key, integral);
                            break;
                        case PLAY_MARK_RESPONSIBILITY_JI://责任鸡
                            break;
                        case PLAY_MARK_PENG_RESPONSIBILITY_JI://碰冲锋鸡
                            //打出冲锋鸡玩家给两倍，其它玩家给一倍，
                            integral = ((baseScore * multiple) * (room.joinedPlayers - 1)) * 1 + (baseScore * multiple);
                            cunPlay.incIntegral(mark.key, integral);
                            break;
                        case PLAY_MARK_ORDINARY_JI://普通幺鸡
                            integral = ((baseScore * multiple) * (room.joinedPlayers - 1)) * 1;
                            cunPlay.incIntegral(mark.key, integral);
                            break;
                        case PLAY_MARK_CATCH_JI://捉到的鸡
                            integral = ((baseScore * multiple) * (room.joinedPlayers - 1)) * 1;
                            cunPlay.incIntegral(mark.key, integral);
                            break;
                        case PLAY_MARK_GOLD_JI://金鸡
                        case PLAY_MARK_BLACK_JI://乌骨鸡
                            integral = ((baseScore * multiple) * (room.joinedPlayers - 1)) * 5;
                            cunPlay.incIntegral(mark.key, integral);
                            break;
                        default:
                            break;
                    }
                });
            }
            cunPlay.marks.forEach(function (mark) {
                switch (mark.key) {
                    case PLAY_MARK_RESPONSIBILITY_JI://责任鸡
                        isResponsibilityJi = true;
                        break;
                    case PLAY_MARK_PENG_RESPONSIBILITY_JI://碰冲锋鸡
                        isPengChargeJi = true;
                    default:
                        break;
                }
            });
            for (var pos in room.players) {
                if(!room.players.hasOwnProperty(pos)){
                    continue;
                }
                var play = room.players[pos];
                //检查其它玩家鸡和豆并当前玩家的扣积分，未听牌时鸡和豆不计分，跳过
                if (cunPlay == play) {
                    continue;
                }
                play.marks.forEach(function (mark) {
                    switch (mark.key) {
                        case PLAY_MARK_CHARGE_JI://冲锋鸡
                            integral = (baseScore * multiple)  * 2;
                            if (play.isTing()) {//对方听牌
                                cunPlay.decIntegral(mark.key, integral);
                            }else if(cunPlay.isTing()){
                                //对方没有听牌并且自己听牌
                                cunPlay.incIntegral(mark.key, integral);
                                play.decIntegral(mark.key, integral);
                            }
                            break;

                        case PLAY_MARK_RESPONSIBILITY_JI://责任鸡
                            //对方有责任鸡并且自己也有碰冲锋鸡的标示
                            if (isPengChargeJi) {
                                integral = (baseScore * multiple) * 2;
                                if (cunPlay.isTing()) {
                                    play.decIntegral(mark.key, integral);
                                }else if(play.isTing()){
                                    cunPlay.decIntegral(mark.key, integral);
                                    play.incIntegral(mark.key, integral);
                                }
                            }
                            break;
                        case PLAY_MARK_PENG_RESPONSIBILITY_JI://碰冲锋鸡
                            //对方有碰冲锋鸡的标示并且自己也有责任鸡标示两倍，否则一倍
                            if (isResponsibilityJi) {
                                return;
                            }
                            integral = (baseScore * multiple) * 1;
                            if(play.isTing()) {
                                cunPlay.decIntegral(mark.key, integral);
                            }else if(cunPlay.isTing()){
                                cunPlay.incIntegral(mark.key, integral);
                                play.decIntegral(mark.key, integral);
                            }
                            break;
                        case PLAY_MARK_ORDINARY_JI://普通幺鸡
                            integral = (baseScore * multiple) * 1;
                            if(play.isTing()) {
                                cunPlay.decIntegral(mark.key, integral);
                            }else if(cunPlay.isTing()) {
                                cunPlay.incIntegral(mark.key, integral);
                                play.decIntegral(mark.key, integral);
                            }
                            break;
                        case PLAY_MARK_CATCH_JI://捉到的鸡
                            if(play.isTing()) {
                                integral = (baseScore * multiple) * 1;
                                cunPlay.decIntegral(mark.key, integral);
                            }
                            break;
                        case PLAY_MARK_GOLD_JI://金鸡
                        case PLAY_MARK_BLACK_JI://乌骨
                            if(play.isTing()) {
                                integral = (baseScore * multiple) * 5;
                                cunPlay.decIntegral(mark.key, integral);
                            }
                            break;
                        default:
                            break;
                    }
                });
            }
        }
    },

    /**
     *  结算杠牌
     */
    settlementGangCardAction: function () {
        var room = this.owner;
        if(!room) {
            return ;
        }
        var baseScore = room.game.baseScore;    // 底分
        var multiple = room.game.multiple;      // 番数
        for (var key in room.players) {
            if (!room.players.hasOwnProperty(key)) {
                continue;
            }
            var cunPlay = room.players[key];
            var cunDianGangCards = [];
            var integral = 0;

            //玩家听牌， 并且没有热炮
            if (cunPlay.isTing() && !cunPlay.hasHuPaiMethod(mjMark.HUCARD_METHOD_REPAO)) {
                //听牌条件下，检查自己的鸡和豆并结算积分
                cunPlay.marks.forEach(function (mark) {
                    switch (mark.key) {
                        case mjMark.PLAY_MARK_DARK_GANG://暗杠、闷豆
                        case mjMark.PLAY_MARK_ADD_GANG://加杠、爬坡豆
                            integral = ((baseScore * multiple) * (room.joinedPlayers - 1)) * 3;
                            cunPlay.incIntegral(mark.key, integral);
                            break;
                        case mjMark.PLAY_MARK_APPOINT_GANG://点杠、点豆
                            integral = (baseScore * multiple) * 3;
                            cunPlay.incIntegral(mark.key, integral);
                            break;
                        case mjMark.PLAY_MARK_PASSIVE_GANG://被点杠、被点豆
                            //别的玩家身上判断是否听牌后再做处理
                            cunDianGangCards.push(mark);
                            break;
                        default:
                            break;
                    }
                });
            }

            for (var pos in room.players) {
                if(!room.players.hasOwnProperty(pos)){
                    continue;
                }
                var play = room.players[pos];
                //检查其它玩家鸡和豆并当前玩家的扣积分，未听牌时鸡和豆不计分，跳过
                if (cunPlay == play) {
                    continue;
                }
                play.marks.forEach(function (mark) {
                    switch (mark.key) {
                        case mjMark.PLAY_MARK_DARK_GANG://暗杠、闷豆
                        case mjMark.PLAY_MARK_ADD_GANG://加杠、爬坡豆
                            if(play.isTing()) {
                                integral = (baseScore * multiple) * 3;
                                cunPlay.decIntegral(mark.key, integral);
                            }
                            break;
                        case mjMark.PLAY_MARK_APPOINT_GANG://点杠、点豆
                            //其它玩家点杠了当前玩家的牌
                            if(play.isTing()) {
                                cunDianGangCards.forEach(function (card) {
                                    if (card.key == mjMark.PLAY_MARK_PASSIVE_GANG && card.value == mark.value) {
                                        integral = (baseScore * multiple) * 3;
                                        cunPlay.decIntegral(mark.key, integral);
                                    }
                                });
                            }
                            break;

                        case mjMark.PLAY_MARK_PASSIVE_GANG://被点杠、被点豆
                            //mjMark.PLAY_MARK_APPOINT_GANG已经处理了
                            break;
                        default:
                            break;
                    }
                });
            }
        }
    },

    /**
     * 结算玩家鸡的总分数
     * @param ePlayer   玩家
     * @returns {*}
     */
    onSettlementJitotalAction: function(ePlayer){

        var jiScore = 0;
        for(var key in ePlayer.settlementMarks){
            if(!ePlayer.settlementMarks.hasOwnProperty(key)){
                continue;
            }
            switch (+key){
                case PLAY_MARK_CHARGE_JI:
                case PLAY_MARK_RESPONSIBILITY_JI:
                case PLAY_MARK_PENG_RESPONSIBILITY_JI:
                case PLAY_MARK_ORDINARY_JI:
                case PLAY_MARK_CATCH_JI:
                case PLAY_MARK_GOLD_JI:
                case PLAY_MARK_BLACK_JI:
                    jiScore += ePlayer.settlementMarks[key];
                    break;
            }
        }
        return jiScore;
    },

    /**
     * 结算玩家杠的总分数
     * @param ePlayer   玩家
     * @returns {*}
     */
    onSettlementGangtotalAction: function(ePlayer){

        var gangScore = 0;
        for(var key in ePlayer.settlementMarks){
            if(!ePlayer.settlementMarks.hasOwnProperty(key)){
                continue;
            }
            switch (+key){
                case mjMark.PLAY_MARK_DARK_GANG             ://暗杠、闷豆
                case mjMark.PLAY_MARK_ADD_GANG              ://加杠、爬坡豆
                case mjMark.PLAY_MARK_APPOINT_GANG          ://点杠、点豆
                case mjMark.PLAY_MARK_PASSIVE_GANG          ://被点杠、被点豆
                    gangScore += ePlayer.settlementMarks[key];
                    break;
            }
        }
        return gangScore;
    },

};

exports.SubGame = SubGame;